package com.ruoyi.server.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.server.domain.vo.IotOrderVo;
import com.ruoyi.server.domain.bo.IotOrderBo;
import com.ruoyi.server.service.IIotOrderService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 指令
 *
 * @author shanzhuo
 * @date 2023-11-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iot/order")
public class IotOrderController extends BaseController {

    private final IIotOrderService iIotOrderService;

    /**
     * 查询指令列表
     */
    @SaCheckPermission("server:order:list")
    @GetMapping("/list")
    public TableDataInfo<IotOrderVo> list(IotOrderBo bo, PageQuery pageQuery) {
        return iIotOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出指令列表
     */
    @SaCheckPermission("server:order:export")
    @Log(title = "指令", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(IotOrderBo bo, HttpServletResponse response) {
        List<IotOrderVo> list = iIotOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "指令", IotOrderVo.class, response);
    }

    /**
     * 获取指令详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("server:order:query")
    @GetMapping("/{id}")
    public R<IotOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iIotOrderService.queryById(id));
    }

    /**
     * 新增指令
     */
    @SaCheckPermission("server:order:add")
    @Log(title = "指令", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody IotOrderBo bo) {
        return toAjax(iIotOrderService.insertByBo(bo));
    }

    /**
     * 修改指令
     */
    @SaCheckPermission("server:order:edit")
    @Log(title = "指令", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody IotOrderBo bo) {
        return toAjax(iIotOrderService.updateByBo(bo));
    }

    /**
     * 删除指令
     *
     * @param ids 主键串
     */
    @SaCheckPermission("server:order:remove")
    @Log(title = "指令", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iIotOrderService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
